<template>
	<view class="u-select">
		<u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto"
			:safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
			<view class="u-select">
				<view class="u-select__header" @touchmove.stop.prevent="">
					<view class="u-select__header__cancel u-select__header__btn" :style="{ color: cancelColor }"
						hover-class="u-hover-class" :hover-stay-time="150" @tap="getResult('cancel')">
						{{cancelText}}
					</view>
					<view class="u-select__header__title">
						{{title}}
					</view>
					<view class="u-select__header__confirm u-select__header__btn"
						:style="{ color: moving ? cancelColor : confirmColor }" hover-class="u-hover-class"
						:hover-stay-time="150" @touchmove.stop="" @tap.stop="getResult('confirm')">
						{{confirmText}}
					</view>
				</view>
				<view class="u-select__body">
					<view class="filter-search">
						<u-input v-model="filterValue" placeholder="请输入关键字进行过滤"></u-input>
					</view>
					<picker-view @change="columnChange" class="u-select__body__picker-view" :value="defaultSelector"
						@pickstart="pickstart" @pickend="pickend">
						<picker-view-column v-for="(item, index) in columnData" :key="index">
							<view class="u-select__body__picker-view__item" v-for="(item1, index1) in item"
								:key="index1">
								<view class="u-line-1">{{getValue(item1) }} </view>
								<!-- {{item1[labelName]}} -->
							</view>
						</picker-view-column>
					</picker-view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	/**
	 * select 列选择器
	 * @description 此选择器用于单列，多列，多列联动的选择场景。(从1.3.0版本起，不建议使用Picker组件的单列和多列模式，Select组件是专门为列选择而构造的组件，更简单易用。)
	 * @tutorial http://uviewui.com/components/select.html
	 * @property {String} mode 模式选择，"single-column"-单列模式，"mutil-column"-多列模式，"mutil-column-auto"-多列联动模式
	 * @property {Array} list 列数据，数组形式，见官网说明
	 * @property {Boolean} v-model 布尔值变量，用于控制选择器的弹出与收起
	 * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
	 * @property {String} cancel-color 取消按钮的颜色（默认#606266）
	 * @property {String} confirm-color 确认按钮的颜色(默认#2979ff)
	 * @property {String} confirm-text 确认按钮的文字
	 * @property {String} cancel-text 取消按钮的文字
	 * @property {String} default-value 提供的默认选中的下标，见官网说明
	 * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
	 * @property {String Number} z-index 弹出时的z-index值(默认10075)
	 * @property {String} value-name 自定义list数据的value属性名 1.3.6
	 * @property {String} label-name 自定义list数据的label属性名 1.3.6
	 * @property {String} child-name 自定义list数据的children属性名，只对多列联动模式有效 1.3.7
	 * @event {Function} confirm 点击确定按钮，返回当前选择的值
	 * @example <u-select v-model="show" :list="list"></u-select>
	 */
	import debounce from '@/uview-ui/libs/function/debounce.js'
	export default {
		props: {
			// 列数据
			list: {
				type: Array,
				default () {
					return [];
				}
			},
			// 是否显示边框
			border: {
				type: Boolean,
				default: true
			},
			// 通过双向绑定控制组件的弹出与收起
			value: {
				type: Boolean,
				default: false
			},
			// "取消"按钮的颜色
			cancelColor: {
				type: String,
				default: '#606266'
			},
			// "确定"按钮的颜色
			confirmColor: {
				type: String,
				default: '#2979ff'
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			},
			safeAreaInsetBottom: {
				type: Boolean,
				default: false
			},
			// 是否允许通过点击遮罩关闭Picker
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 提供的默认选中的下标
			defaultValue: {
				type: Array,
				default () {
					return [0];
				}
			},
			// 模式选择，single-column-单列，mutil-column-多列，mutil-column-auto-多列联动
			mode: {
				type: String,
				default: 'single-column'
			},
			// 自定义value属性名
			valueName: {
				type: String,
				default: 'value'
			},
			// 自定义label属性名
			labelName: {
				type: String,
				default: 'label'
			},
			// 自定义多列联动模式的children属性名
			childName: {
				type: String,
				default: 'children'
			},
			// 顶部标题
			title: {
				type: String,
				default: ''
			},
			// 取消按钮的文字
			cancelText: {
				type: String,
				default: '取消'
			},
			// 确认按钮的文字
			confirmText: {
				type: String,
				default: '确认'
			},
			// 是否拼接 label value 
			isConcat: {
				type: Boolean,
				default: false
			},
			// 拼接label、value符号
			concatSymbol: {
				type: String,
				default: ' | '
			},
		},
		data() {
			return {
				// 用于列改变时，保存当前的索引，下一次变化时比较得出是哪一列发生了变化
				defaultSelector: [0],
				// picker-view的数据
				columnData: [],
				// 每次队列发生变化时，保存选择的结果
				selectValue: [],
				// 列数
				columnNum: 1,
				// 列是否还在滑动中，微信小程序如果在滑动中就点确定，结果可能不准确
				moving: false,
				copyList: [],
				filterList: [],
				sliceIndexStart: 0, // 懒加载数据 数组start
				sliceIndexEnd: 20, // 懒加载数据 数组end
				filterValue: '' // 关键字过滤
			};
		},
		watch: {
			// 在select弹起的时候，重新初始化所有数据
			value: {
				immediate: true,
				handler(val) {
					if (val) setTimeout(() => {
							this.sliceIndexStart = 0,
							this.sliceIndexEnd = 20,
							this.init();
					}, 10);
				}
			},
			filterValue(val) {
				if (val) {
					this.filterData(val)
				} else {
					// this.defaultValue=[0]
					this.$emit("confirm",[{extra:0}])
					this.sliceIndexStart = 0,
					this.sliceIndexEnd = 20,
				this.setDefaultSelector();
				this.setColumnData(true);
				this.setSelectValue();
				}
			},
			defaultValue: {
				handler(val) {
				},
				immediate: true,
			}
		},
		computed: {
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			},
			getValue() {
				return (item => {
					if (this.isConcat) {
						return item[this.labelName] + this.concatSymbol + item[this.valueName]
					} else {
						return item[this.labelName]
					}
				})
			}
		},
		methods: {
			// 过滤数据
			filterData(val, echo) {
				let _this = this
				debounce(() => {
					let arr = _this.copyList.filter((item, index) => {
						if (item[_this.labelName].indexOf(val) > -1 || item[_this.valueName].indexOf(val) >
							-1) {

							return item
						}
					})
					if (echo) {
						arr.some((item, index) => {
							if (item[_this.valueName] == _this.selectValue[0].value) {
								_this.defaultSelector = [index];
								_this.sliceIndexStart=index;
								_this.sliceIndexEnd = index+20
							}
						})
					}
					arr = arr.map((item, index) => {
						return {
							...item,
							extra: index
						}
					})
					_this.filterList = arr
					_this.$set(_this.columnData, 0, arr);	
					if(arr.length>0 && this.defaultSelector[0] ==0)this.selectValue = [{label:arr[0][this.labelName], value :arr[0][this.valueName]}]
				})

			},
			// 标识滑动开始，只有微信小程序才有这样的事件
			pickstart() {
				// #ifdef MP-WEIXIN
				this.moving = true;
				// #endif
			},
			// 标识滑动结束
			pickend() {
				// #ifdef MP-WEIXIN
				this.moving = false;
				// #endif
			},
			init() {
				this.setDefaultSelector();
				this.setColumnData();
				this.setSelectValue();
			},
			// 获取默认选中列下标
			setDefaultSelector() {
				this.defaultSelector = this.defaultValue.length == this.columnNum ? this.defaultValue : Array(this
					.columnNum).fill(0);
				// if (this.defaultSelector[0] != 0 && this.filterValue) {
				// 	this.defaultSelector = [this.defaultSelector[0] - 1]
				// }
			},

			// 获取需要展示在picker中的列数据
			setColumnData(flag) {
				let data = [];
				this.selectValue = [];
				if (this.mode == 'single-column') {
					data = this.list;
				}
				this.copyList = data;

				const _this = this
				const arr = data.slice(this.sliceIndexStart, this.sliceIndexEnd)
				if (_this.defaultSelector[0] == 0 && !this.filterValue) {
					_this.columnData = [arr];
					this.defaultSelector = [0]
					
				} else if (this.filterValue) {
					this.filterData(this.filterValue, true)
				} else {
					if(flag){
						_this.columnData = [arr];
					}else{
						this.list.filter((item, index) => {
							item.extra = index
							if (item[_this.valueName] === _this.list[_this.defaultSelector[0]][_this.valueName] ) {
								arr.some((item2,index2) => {
									if(item2[_this.valueName] === item[_this.valueName]){
										arr.splice(index2,1)
										return ;
									}
								})
								arr.unshift(item)
								_this.columnData = [arr];
								return;
							}
						})
					}
					this.defaultSelector = [0]
				}
				
			},
			// 获取默认选中的值，如果没有设置defaultValue，就默认选中每列的第一个
			setSelectValue() {
				let tmp = null;
				for(let i=0;i<this.columnNum;i++){
					tmp = this.columnData[i][this.defaultSelector[i]];
					// tmp = this.copyList[this.defaultSelector[i]];
					let data = {
						value: tmp ? tmp[this.valueName] : null,
						label: tmp ? tmp[this.labelName] : null
					};
					// 判断是否存在额外的参数，如果存在，就返回
					if (tmp && tmp.extra) data.extra = tmp.extra;
					this.selectValue.push(data)
				}
			},
			// 列选项
			columnChange(e) {
				if (e.target.value[0] === this.columnData[0].length - 1) {
					let list = []
					// 判断是否进行了筛选
					if (this.filterValue) {
						// this.defaultSelector = [e.detail.value[0]];
						list = this.filterList
					} else {
						list = this.copyList;
					}
					this.sliceIndexStart = this.sliceIndexEnd;
					this.sliceIndexEnd = this.sliceIndexStart + 20
					this.$set(this.columnData, 0, [...this.columnData[0], ...list.slice(this.sliceIndexStart, this
						.sliceIndexEnd)])
				}
				let index = null;
				let columnIndex = e.detail.value;
				// this.defaultSelector=[e.detail.value[0]]
				// 由于后面是需要push进数组的，所以需要先清空数组
				this.selectValue = [];
				let data = this.columnData[0][columnIndex[0]];
				// let data = this.copyList[columnIndex[0]];
				// 初始默认选中值
				let tmp = {
					value: data ? data[this.valueName] : null,
					label: data ? data[this.labelName] : null,
				};
				// 判断是否有需要额外携带的参数
				if (data && data.extra !== undefined) tmp.extra = data.extra;
				this.selectValue.push(tmp);
			},
			close() {
				this.$emit('input', false);
			},
			// 点击确定或者取消
			getResult(event = null) {
				// #ifdef MP-WEIXIN
				if (this.moving) return;
				// #endif
				if (event) this.$emit(event,   this.selectValue);
				this.close();
			},
			selectHandler() {
				this.$emit('click');
			}
		}
	};
</script>

<style scoped lang="scss">
	@import "@/uview-ui/libs/css/style.components.scss";

	.filter-search {
		padding: 0rpx 40rpx;
	}

	.u-select {

		&__action {
			position: relative;
			line-height: $u-form-item-height;
			height: $u-form-item-height;

			&__icon {
				position: absolute;
				right: 20rpx;
				top: 50%;
				transition: transform .4s;
				transform: translateY(-50%);
				z-index: 1;

				&--reverse {
					transform: rotate(-180deg) translateY(50%);
				}
			}
		}

		&__hader {
			&__title {
				color: $u-content-color;
			}
		}

		&--border {
			border-radius: 6rpx;
			border-radius: 4px;
			border: 1px solid $u-form-item-border-color;
		}

		&__header {
			@include vue-flex;
			align-items: center;
			justify-content: space-between;
			height: 80rpx;
			padding: 0 40rpx;
		}

		&__body {
			width: 100%;
			height: 500rpx;
			overflow: hidden;
			background-color: #fff;

			&__picker-view {
				height: 100%;
				box-sizing: border-box;

				&__item {
					@include vue-flex;
					align-items: center;
					justify-content: center;
					font-size: 32rpx;
					color: $u-main-color;
					padding: 0 8rpx;
				}
			}
		}
	}
</style>